package test;

import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 优化sql
 */
public class DeleteFilesHandle {
    private static String SOURCE_DIR = "e:\\del_images_03\\";

    /**
     * 获取所有图片名称
     * @param path
     * @param listFileName
     */
    public static void getAllFileName(String path, List<String> listFileName) {
        File file = new File(path);
        File[] files = file.listFiles();
        String[] names = file.list();
        if (names != null) {
            String[] completNames = new String[names.length];
            for (int i = 0; i < names.length; i++) {
                    completNames[i] = names[i];
            }
            listFileName.addAll(Arrays.asList(completNames));
        }
        for (File a : files) {
            if (a.isDirectory()) {//如果文件夹下有子文件夹，获取子文件夹下的所有文件全路径。
                getAllFileName(a.getAbsolutePath() + "\\", listFileName);
            }
        }
    }

    /**
     * 读入TXT文件
     */
    public static String readFile() {
        String pathname = "e:\\" + "imageSql.txt"; // 绝对路径或相对路径都可以，写入文件时演示相对路径,读取以上路径的input.txt文件
        //防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw;
        //不关闭文件会导致资源的泄露，读写文件都同理
        //Java7的try-with-resources可以优雅关闭文件，异常时自动关闭文件；详细解读https://stackoverflow.com/a/12665271
        StringBuffer sb = new StringBuffer();
        try (FileReader reader = new FileReader(pathname);
             BufferedReader br = new BufferedReader(reader) // 建立一个对象，它把文件内容转成计算机能读懂的语言
        ) {
            String line;
            //网友推荐更加简洁的写法
            while ((line = br.readLine()) != null) {
                // 一次读入一行数据
                //System.out.println(line);
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        List<String> listFileName = new ArrayList<>();
        getAllFileName(SOURCE_DIR, listFileName);
        // 读取file‘
        String data = readFile();
        String[] arr =  new String[1];
        if(StringUtils.isNotBlank(data)){
            arr = data.split("\\;");
        }
        List<String> sqls = new ArrayList<>(Arrays.asList(arr));

        for (int i = 0; i < listFileName.size(); i++) {
            String name = listFileName.get(i);
            for (int j = 0; j < sqls.size(); j++) {
                if (sqls.get(j).contains(name)) {
                    sqls.remove(j);
                    j--;
                }
            }
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < sqls.size(); i++) {
            sb.append(sqls.get(i).replace("--", ""));
            sb.append(" ; \r\n");
        }

        writeFile(sb);

    }

    /**
     * 写入TXT文件
     */
    public static void writeFile(StringBuffer sb) {
        String sqlTxtFile = "e:\\imageSql_final.txt";
        try {
            File writeName = new File(sqlTxtFile);
            writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
            try (FileWriter writer = new FileWriter(writeName);
                 BufferedWriter out = new BufferedWriter(writer)
            ) {
                out.write(sb.toString()); // \r\n即为换行
                out.flush(); // 把缓存区内容压入文件
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("sql文件写入完成");
    }
}
